ALMaSS  1.0
The Animal, Landscape and Man Simulation System
CropRotation Class Reference

#include <croprotation.h>

Public Member Functions

TTypesOfVegetation GetNextCrop (int a_farmtype, int a_current_crop)
 
TTypesOfVegetation GetFirstCrop (int a_farmtype, bool *a_low_nutrient)
 
 CropRotation (int a_num_crops)
 
 ~CropRotation (void)
 

Private Attributes

vector< Rotation * > m_rots
 
vector< Starter * > m_start
 

Constructor & Destructor Documentation

◆ CropRotation()

CropRotation::CropRotation ( int  a_num_crops)
43 {
44  FILE *inpfile;
45  char filename[50];
46 
47  m_rots.resize( a_num_crops );
48  m_start.resize( a_num_crops );
49 
50  for (int i=0; i<a_num_crops; i++) {
51  m_rots[ i ] = new Rotation;
52  m_start[ i ] = new Starter;
53  }
54 
55  for (int i=0; i<NoFarmTypes; i++) {
56  // Now works for any number of rotations.
57  sprintf( filename, "%sFarmType_%d.rot",
59  i );
60 
61  inpfile = fopen(filename, "r" );
62  if (!inpfile) {
63  g_msg->Warn( WARN_FILE, "CropRotation::CropRotation():"
64  " Unable to open file ", filename);
65  exit(1);
66  }
67  for (int j=0; j<a_num_crops; j++) {
68  fscanf( inpfile, "%d", (int*)&(m_rots[ j ]->CropNum[ i ]) );
69  for (int k=0; k<4; k++) {
70  fscanf( inpfile, "%d", (int*)&m_rots[ j ]->NewCrop[ i ][ k ] );
71  fscanf( inpfile, "%d", &m_rots[ j ]->Percent[ i ][ k ] );
72  }
73  }
74  fclose( inpfile );
75 
76  sprintf( filename, "FarmType_%d.stt", i );
77 
78  inpfile = fopen(filename, "r" );
79  if (!inpfile) {
80  g_msg->Warn( WARN_FILE, "CropRotation::CropRotation():"
81  " Unable to open file ", filename);
82  exit(1);
83  }
84  for (int j=0; j<a_num_crops; j++) {
85  fscanf( inpfile, "%d %d",
86  (int*)&m_start[ j ]->CropNum[ i ],
87  &m_start[ j ]->Percent[ i ] );
88  }
89  fclose(inpfile);
90  }
91 }
const char * value(void)
Definition: configurator.h:152
vector< Starter * > m_start
Definition: croprotation.h:55
vector< Rotation * > m_rots
Definition: croprotation.h:54
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:59
CfgStr l_map_rotation_files_prefix("MAP_ROTATION_FILES_PREFIX", CFG_CUSTOM, "")
const int NoFarmTypes
Definition: croprotation.h:37
class MapErrorMsg * g_msg
This pointer provides access the to the internal ALMaSS error message system.
Definition: maperrormsg.cpp:41
@ WARN_FILE
Definition: maperrormsg.h:37
Definition: croprotation.h:40
Definition: croprotation.h:47

References g_msg, l_map_rotation_files_prefix, m_rots, m_start, NoFarmTypes, CfgStr::value(), MapErrorMsg::Warn(), and WARN_FILE.

◆ ~CropRotation()

CropRotation::~CropRotation ( void  )
96 {
97  for (unsigned int i=0; i<m_rots.size(); i++) {
98  delete m_rots[ i ];
99  delete m_start[ i ];
100  }
101 }

References m_rots, and m_start.

Member Function Documentation

◆ GetFirstCrop()

TTypesOfVegetation CropRotation::GetFirstCrop ( int  a_farmtype,
bool *  a_low_nutrient 
)
127 {
128  int num_crops = g_crops->GetNumCrops();
129 
130  for ( int i=0; i<num_crops; i++ ) {
131  int percent = m_start[ i ]->Percent[ a_farmtype ];
132 
133  if ( (num_crops==i) ||
134  (-1 == m_start[ i+1 ]->Percent[ a_farmtype ]) ||
135  (rand()%100 < percent) ) {
136  *a_low_nutrient = g_crops->GetNutStatus( i );
137  return (TTypesOfVegetation) i;
138  }
139  }
140  g_msg->Warn( WARN_BUG, "CropRotation::GetFirstCrop():"
141  " Unable to recover crop type?!", "");
142  exit(1);
143 }
bool GetNutStatus(int a_plant_num)
Definition: plants.h:170
int GetNumCrops()
Definition: plants.h:135
@ WARN_BUG
Definition: maperrormsg.h:34
class PlantGrowthData * g_crops
Definition: plants.cpp:41
TTypesOfVegetation
Definition: tov_declaration.h:30

References g_crops, g_msg, PlantGrowthData::GetNumCrops(), PlantGrowthData::GetNutStatus(), m_start, MapErrorMsg::Warn(), and WARN_BUG.

◆ GetNextCrop()

TTypesOfVegetation CropRotation::GetNextCrop ( int  a_farmtype,
int  a_current_crop 
)
106 {
107  //int NumCrops = g_crops->GetNumCrops();
108  for ( int i=0; i<4; i++ ) {
109  int percent = m_rots[ a_current_crop ]->Percent[ a_farmtype ][ i ];
110 
111  // Lazy evaluation for experts. ;-)
112  if ( (3==i) ||
113  (-1 == m_rots[ a_current_crop ]->Percent[ a_farmtype ][ i+1 ]) ||
114  (rand()%100 < percent) ) {
115  return m_rots[ a_current_crop ]->NewCrop[ a_farmtype ][ i ];
116  }
117  }
118  g_msg->Warn( WARN_BUG, "CropRotation::GetNextCrop():"
119  " Unable to recover crop type?!", "");
120  exit(1);
121 }

References g_msg, m_rots, MapErrorMsg::Warn(), and WARN_BUG.

Member Data Documentation

◆ m_rots

vector< Rotation* > CropRotation::m_rots
private

◆ m_start

vector< Starter* > CropRotation::m_start
private

The documentation for this class was generated from the following files: